﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Threading;
using Hi_Baidu.Entry;
using Hi_Baidu.Entry.CollectionObject;
using Hi_Baidu.Packet.InPacket;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Timer;
using Hi_Baidu.ListType;
using System.Windows.Media.Imaging;
using Hi_Baidu.Notify.NotifyObject;
using System.Collections;
using Hi_Baidu.MusicPlayer;
using Hi_Baidu.User.LogEntry;
using Hi_Baidu.Cache;

namespace Hi_Baidu.Packet
{
    /// <summary>
    ///     提供了所有回馈包的处理
    /// </summary>
    class PackageProcess
    {
        private delegate void PUBLIC_DELEGATE();
        private delegate void PUBLIC_STRING_DELEGATE(String Text);
        private delegate TreeViewItem ITEM_DELEGATE(Item item);
        private delegate void FRIEND_DELEGATE(String itemId, Friend friend);
        private delegate void UPDATE_FRIEND_DELEGATE(GetFriendInfoReplyPacket inpackage);
        //给定父类
        private delegate void MODIFY_USER_INFO_FAIL(UpdateUserInfoReplyPacket inpackage);
        private delegate void UPDATE_ITEM(UpdateItemReplyPacket inpackage);
        private delegate void GET_POSTFRIEND_INFO_NOTFIND(GetPostFriendInfoNotFind_ReplyPacket inpackage);
        private delegate void GET_POSTFRIEND_INFO(GetPostFriendInfo_ReplyPacket inpackage);
        private delegate void RETURN_ITEM(String ItemKey);
        private delegate void SET_DELETE_ITEM(DeleteItemReplyPacket ItemKey);
        private delegate void SET_ADD_ITEM(AddItemReplyPacket inpackage);
        private delegate void CHANAGE_FRIEND_STATE(Friend friend);
        private delegate void DELETE_FRIEND(DeleteFriendReplyPacket inpackage);

        /// <summary>
        ///     处理登陆回馈包
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.LoginReplyPacket">
        ///     <para>
        ///         登陆回馈包
        ///     </para>
        /// </param>
        public static void Process_REPLY_Login_Package(LoginReplyPacket inpackage)
        {
            //判断登录状态
            if (inpackage.GetLoginState())
            {
                ////////////////////////////////////////////////////////
                ///                    登录成功
                ////////////////////////////////////////////////////////
                Globle.timeoutTimerManager.RemoveTimer("LoginTimerOut");
                //保存自身基础会话码
                Globle.user.SESSION_CODE = inpackage.GetSessionCode();
                //请求自身信息
                GetInfomationModel.GetUserInfo(Encoding.Default.GetBytes(inpackage.GetUsername()));
                //挂接超时器
                GetMySelfInfomation_TimeOutEvent GSTE = new GetMySelfInfomation_TimeOutEvent(inpackage.GetUsername(),
                                                                                             "GetMySelfInfoTimeOut",
                                                                                             5000, 3);
                Globle.timeoutTimerManager.InstallTimer(GSTE);
                Globle.user.USER_ONLINE_STATE = inpackage.GetOnlineState();
                //开始初始化心跳包超时器
                HeartBreathTimeOutEvent heartBreathTimeOutEvent = new HeartBreathTimeOutEvent(inpackage.GetUsername(), "HeartBreath : " + inpackage.GetUsername(), 7000);
                //打入心跳超时器
                Globle.timeoutTimerManager.InstallTimer(heartBreathTimeOutEvent);
            }
            else
            {
                ////////////////////////////////////////////////////////
                ///                    登录失败
                ////////////////////////////////////////////////////////
                Globle.timeoutTimerManager.RemoveTimer("LoginTimerOut");
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_DELEGATE(ShowNotifyWindow));
                Globle.TEMP_USER_SETTING = null;
            }
        }

        /// <summary>
        ///     处理获取自身信息包
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.GetMySelfInfo_ReplyPacket">
        ///     <para>
        ///         自身信息包
        ///     </para>
        /// </param>
        public static void Process_REPLY_GETMYSELFINFO_PACKAG(GetMySelfInfo_ReplyPacket inpackage)
        {
            //得到用户信息字典
            Globle.user.DIC_USERINFO = inpackage.DIC_CONTENT;
            //卸载获取自身信息的超时器
            Globle.timeoutTimerManager.RemoveTimer("GetMySelfInfoTimeOut");
            //开始获取自身分组信息
            GetInfomationModel.GetUserGroupInfo(Globle.user.DIC_USERINFO["USERNAME"]);
            //挂接获取分组信息超时器
            GetGroupTimeOutEvent GTOE = new GetGroupTimeOutEvent(inpackage.GetUsername(), "GetGroupTimeOut", 10000, 2);
            Globle.timeoutTimerManager.InstallTimer(GTOE);
            //开始查询原始登陆过的用户
            TYPE_AppUserInfo AppUserInfo = (TYPE_AppUserInfo)Globle.applicationUserManager.GetMemberByKey(Globle.TEMP_USER_SETTING.Username);
            Globle.TEMP_USER_SETTING.HeadImage = Encoding.Default.GetString(Globle.user.DIC_USERINFO["HEADIMAGE"]);
            //原来一直没有登录过(新号)
            if (AppUserInfo == null)
            {
                Globle.applicationUserManager.Add(new TYPE_AppUserInfo(Globle.TEMP_USER_SETTING.Username));
                ClientProcessModel.SaveApplicationUser();
                //创建新用户的相关资料
                UserProcessModel.ProcessUserReference(Globle.win_Login, Globle.TEMP_USER_SETTING.Username);
            }
            else
            {
                //直接更新
                UserProcessModel.UpdateUserInfoDB(Globle.TEMP_USER_SETTING.Username);
            }
        }

        /// <summary>
        ///     处理获取分组信息反馈包
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.GetGroupReplyPacket">
        ///     <para>
        ///         分组信息反馈包
        ///     </para>
        /// </param>
        public static void Process_REPLY_GROUP_PACKAGE(GetGroupReplyPacket inpackage)
        {
            //卸载获取分组超时器
            Globle.timeoutTimerManager.RemoveTimer("GetGroupTimeOut");
            //创建主面板
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(MainBoardViewModel.CreateInstance));
            //更新自身信息
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(MainBoardViewModel.FlushMyInfo));
            //得到分组名称和对应的分组Id
            String[] Items = inpackage.GetItems();
            String[] ItemsId = inpackage.GetItemsId();
            //判断数目对称
            if(Items.Length == ItemsId.Length)
            {
                for (int i = 0; i < Items.Length; i++)
                {
                    //创建分组
                    Item item = new Item(ItemsId[i], Items[i]);
                    //打入分组集合
                    Globle.itemCollection.Add(item);
                    //获取该分组对应的好友
                    GetInfomationModel.GetUserFriendByItemId(Encoding.Default.GetBytes(inpackage.GetUsername()), BitConverter.GetBytes(int.Parse(ItemsId[i])));
                    //创建相应获取好友的超时器 [Timer's Key == Item.Key]
                    GetFriendByGroup_TimeOutEvent GFTE = new GetFriendByGroup_TimeOutEvent(Encoding.Default.GetBytes(inpackage.GetUsername()),
                                                                                           BitConverter.GetBytes(int.Parse(ItemsId[i])),
                                                                                           "GetFriendByGroupTimeOut-" +
                                                                                           ItemsId[i], 10000, 1);
                    //安装超时器
                    Globle.timeoutTimerManager.InstallTimer(GFTE);
                    //向好友树加入新分组
                    System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ITEM_DELEGATE(MainBoardViewModel.AddItem), item);
                }
            }
            else
            {
                throw new Exception("分组数目与分组Id数目不对称!");
            }
        }

        /// <summary>
        ///     处理获取指定分组下好友信息反馈包
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.GetFriendByGroupIdReplyPacket">
        ///     <para>
        ///         指定分组下好友信息反馈包
        ///     </para>
        /// </param>
        public static void Process_REPLY_FRIEND_BY_GROUPID_PACKAGE(GetFriendByGroupIdReplyPacket inpackage)
        {
            Globle.timeoutTimerManager.RemoveTimer("GetFriendByGroupTimeOut-" + inpackage.GetItemId());
            if (inpackage.HasFriend())
            {
                //获得改组所有好友帐号和ID
                String[] Friends = inpackage.GetFriends();
                String[] FriendsId = inpackage.GetFriendsId();
                if (Friends.Length == FriendsId.Length)
                {
                    for (int i = 0; i < Friends.Length; i++)
                    {
                        //开始创建好友对象
                        Friend friend = new Friend(Friends[i]);
                        friend.OnlineState = true;
                        friend.Username = Friends[i];
                        friend.ForItem = (Item)Globle.itemCollection.GetMemberByKey(inpackage.GetItemId());
                        //加入该好友
                        Globle.friendCollection.Add(friend); 
                        //开始向服务器提交获取请求
                        GetInfomationModel.GetFriendInfo(Encoding.Default.GetBytes(inpackage.GetUsername()), BitConverter.GetBytes(int.Parse(FriendsId[i])));
                        ////////////////////////////////////////////////////////////////
                        ///    开始为每个好友获取详细信息的动作创建超时器
                        ////////////////////////////////////////////////////////////////
                        //创建超时器
                        GetFriendInfoTimeOutEvent GFIT = new GetFriendInfoTimeOutEvent(Encoding.Default.GetBytes(inpackage.GetUsername()), BitConverter.GetBytes(int.Parse(FriendsId[i])), FriendsId[i], 10000, 2);
                        //安装超时器
                        Globle.timeoutTimerManager.InstallTimer(GFIT);
                        //向指定分组下加入好友
                        System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new FRIEND_DELEGATE(MainBoardViewModel.AddFriend), friend.ForItem.Key, new Object[] { friend });
                    }
                }
                else
                {
                    throw new Exception("好友帐号数目与ID数目不匹配");
                }
            }
            if ((Globle.timeoutTimerManager.Count() >= 0 && Globle.timeoutTimerManager.Count() < 2) && Globle.timeoutTimerManager.GetMemberByKey("HeartBreath : " + Encoding.Default.GetString(Globle.user.DIC_USERINFO["USERNAME"])) != null)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(MainBoardViewModel.Show));
            }
        }

        /// <summary>
        ///     处理获取指定好友详细信息反馈包
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.GetFriendInfoReplyPacket">
        ///     <para>
        ///         指定好友详细信息反馈包
        ///     </para>
        /// </param>
        public static void Process_REPLY_FRIEND_INFO_PACKAGE(GetFriendInfoReplyPacket inpackage)
        {
            Globle.timeoutTimerManager.RemoveTimer(inpackage.GetFriendId());
            if ((Globle.timeoutTimerManager.Count() >= 0 && Globle.timeoutTimerManager.Count() < 3) && Globle.timeoutTimerManager.GetMemberByKey("HeartBreath : " + Encoding.Default.GetString(Globle.user.DIC_USERINFO["USERNAME"])) != null)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(MainBoardViewModel.Show));
            }
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new UPDATE_FRIEND_DELEGATE(MainBoardViewModel.UpdateFriend), inpackage);
        }

        /// <summary>
        ///     处理获取修改个性签名结果的反馈包
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.ModifyProdReplyPacket">
        ///     <para>
        ///         修改个性签名结果的反馈包
        ///     </para>
        /// </param>
        public static void Process_REPLY_MODIFYPROD_PACKAGE(ModifyProdReplyPacket inpackage)
        {
            //卸载修改个性签名超时器
            Globle.timeoutTimerManager.RemoveTimer("ModifyProdTimeOut");
            if (inpackage.GetModifyState())
            {
                ////////////////////////////////////////////////////////
                ///                     修改成功
                ////////////////////////////////////////////////////////
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_DELEGATE(MainBoardViewModel.UpdateMyProd));
            }
            else
            {
                ////////////////////////////////////////////////////////
                ///                     修改失败
                ////////////////////////////////////////////////////////
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new PUBLIC_STRING_DELEGATE(ShowNotifyWindow), "更新个性签名失败 !");
            }
        }

        public static void Process_UPDATE_USERINFO_PACKAGE(UpdateUserInfoReplyPacket inpackage)
        {
            //卸载相应超时器
            Globle.timeoutTimerManager.RemoveTimer("UpdateChanage-" + inpackage.GetDbFiled());
            //如果更新失败
            if (!inpackage.GetUpdateState())
            {
                System.Windows.Application.Current.Dispatcher.Invoke
                    (
                        DispatcherPriority.Background, 
                        new MODIFY_USER_INFO_FAIL(ModifyUserInfoToOrg),
                        inpackage
                    );
            }
            //更新成功
            else
            {
                System.Windows.Application.Current.Dispatcher.Invoke
                    (
                        DispatcherPriority.Background, 
                        new MODIFY_USER_INFO_FAIL(SetUserInfo), 
                        inpackage
                    );
            }
        }

        #region 提示错误框

        private static void ShowNotifyWindow()
        {
            LoginProcessModel.ChangeState(false, Globle.win_Login);
            Win_Notify WN = new Win_Notify();
            WN.Owner = Globle.win_Login;
            WN.tb_Notify.Text = "登录失败, 请确认账号或者密码正确 !";
            WN.ShowDialog();
        }

        private static void ShowNotifyWindow(String NotifyText)
        {
            LoginProcessModel.ChangeState(false, Globle.win_Login);
            Win_Notify WN = new Win_Notify();
            WN.Owner = Globle.win_Login;
            WN.tb_Notify.Text = NotifyText;
            WN.ShowDialog();
        }

        #endregion

        /// <summary>
        ///     更新用户信息失败后，将原来的信息还原
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.UpdateUserInfoReplyPacket">
        ///     <para>
        ///         更新用户包
        ///     </para>
        /// </param>
        private static void ModifyUserInfoToOrg(UpdateUserInfoReplyPacket inpackage)
        {
            System.Windows.Application.Current.Dispatcher.Invoke(
                    DispatcherPriority.Background,
                    new PUBLIC_STRING_DELEGATE(ShowNotifyWindow),
                    "网络问题，个人信息更新失败 !"
                    );
            //获得个人信息资料窗口
            IMetaData modifyWindow = Globle.popWindowManager.GetMemberByKey("ModifyUserInfo");
            if (modifyWindow != null)
            {
                switch (inpackage.GetDbFiled())
                {
                    //昵称
                    case "NICKNAME":
                        (modifyWindow as Win_ModifyUserInfo).txt_NickName.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["NICKNAME"]);
                        break;
                    //个性签名
                    case "PROD":
                        (modifyWindow as Win_ModifyUserInfo).txt_Prod.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["PROD"]);
                        break;
                    //真实姓名
                    case "NAME":
                        (modifyWindow as Win_ModifyUserInfo).txt_Name.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["NAME"]);
                        break;
                    //性别
                    case "SEX":
                        if (Encoding.Default.GetString(Globle.user.DIC_USERINFO["SEX"]) == "0")
                        {
                            (modifyWindow as Win_ModifyUserInfo).RB_Man.IsChecked = true;
                        }
                        else
                        {
                            (modifyWindow as Win_ModifyUserInfo).RB_Woman.IsChecked = true;
                        }
                        break;
                    //电话
                    case "TEL":
                        (modifyWindow as Win_ModifyUserInfo).txt_Tel.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["TEL"]);
                        break;
                    //电子邮箱
                    case "EMAL":
                        (modifyWindow as Win_ModifyUserInfo).txt_Email.Text = Encoding.Default.GetString(Globle.user.DIC_USERINFO["EMAL"]);
                        break;
                    //密码
                    case "PASSWORD":
                        (modifyWindow as Win_ModifyUserInfo).txt_NewPassword.Password = (modifyWindow as Win_ModifyUserInfo).txt_ConfigPassword.Password = "";
                        break;
                    //头像
                    case "HEADIMAGE":
                        (modifyWindow as Win_ModifyUserInfo).Img_HeadImage.Source = new BitmapImage(new Uri("pack://application:,,,/syshead/" + Encoding.Default.GetString(Globle.user.DIC_USERINFO["HEADIMAGE"]) + ".jpg"));
                        break;
                }
            }
        }

        /// <summary>
        ///     更新用户信息成功后，将新信息覆盖到原来的信息
        /// </summary>
        /// <param name="inpackage" type="Hi_Baidu.Packet.InPacket.UpdateUserInfoReplyPacket">
        ///     <para>
        ///         更新用户包
        ///     </para>
        /// </param>
        private static void SetUserInfo(UpdateUserInfoReplyPacket inpackage)
        {
              //获得个人信息资料窗口
            IMetaData modifyWindow = Globle.popWindowManager.GetMemberByKey("ModifyUserInfo");
            if (modifyWindow != null)
            {
                switch (inpackage.GetDbFiled())
                {
                    //昵称
                    case "NickName":
                        Globle.user.DIC_USERINFO["NICKNAME"]    = GetNewValueInUserInfo("NickName");
                        Globle.win_Main.tb_Username.Text       = Encoding.Default.GetString(Globle.user.DIC_USERINFO["NICKNAME"]);
                        break;
                    //个性签名
                    case "Prod":
                        Globle.user.DIC_USERINFO["PROD"]            = GetNewValueInUserInfo("Prod");
                        Globle.win_Main.tb_UserProd.Text        = Encoding.Default.GetString(Globle.user.DIC_USERINFO["PROD"]);
                        break;
                    //真实姓名
                    case "Name":
                        Globle.user.DIC_USERINFO["NAME"]           = GetNewValueInUserInfo("Name");
                        break;
                    //性别
                    case "Sex":
                        if (Encoding.Default.GetString(Globle.user.DIC_USERINFO["SEX"]) == "0")
                        {
                            Globle.user.DIC_USERINFO["SEX"]           =  Encoding.Default.GetBytes("1");
                        }
                        else
                        {
                            Globle.user.DIC_USERINFO["SEX"]           = Encoding.Default.GetBytes("0");
                        }
                        break;
                    //电话
                    case "Tel":
                        Globle.user.DIC_USERINFO["TEL"]               = GetNewValueInUserInfo("Tel");
                        break;
                    //电子邮箱
                    case "Emal":
                        Globle.user.DIC_USERINFO["EMAL"]           = GetNewValueInUserInfo("Emal");
                        break;
                    //密码
                    case "Password":
                        Globle.user.DIC_USERINFO["PASSWORD"]  = GetNewValueInUserInfo("Password");
                        break;
                    //用户头像
                    case "HeadImage":
                        Globle.user.DIC_USERINFO["HEADIMAGE"] = GetNewValueInUserInfo("HeadImage");
                        Globle.win_Main.Img_UserHead.Source = new BitmapImage
                            (
                                new Uri
                                    (
                                        "pack://application:,,,/syshead/" + Encoding.Default.GetString(Globle.user.DIC_USERINFO["HEADIMAGE"]) + "_l.jpg"
                                    )
                            );
                        //广播更新用户头像到所有聊天窗口
                        UserViewModel.ChangeHeadImageToTalkWindow(Encoding.Default.GetString(Globle.user.DIC_USERINFO["HEADIMAGE"]));
                        break;
                }
            }
        }

        /// <summary>
        ///     处理当前强制下线包
        /// </summary>
        public static void Process_User_Set_Offline(SetOfflinePacket inpackage)
        {
            if (inpackage.GetSessionCode() == Globle.user.SESSION_CODE)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_DELEGATE(
                                                                         UserProcessModel.PROCESS_SET_OFFLINE));
            }
        }

        /// <summary>
        ///     处理更改用户在线状态请求回复包
        /// </summary>
        /// <param name="inpackage"></param>
        public static void Process_REPLY_CHANAGE_USER_ONLINESTATE(ChanageUserOnlineStateReplyPacket inpackage)
        {
            //更改成功
            if(inpackage.GetChanageState())
            {
                //卸载超时器
                Globle.timeoutTimerManager.RemoveTimer("ChanageUserOnlineStateTimeOut");
                //保存登录状态
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_DELEGATE(
                                                                         UserProcessModel.SetUserOnlineState));
            }
            //更改失败
            else
            {
                //卸载超时器
                Globle.timeoutTimerManager.RemoveTimer("ChanageUserOnlineStateTimeOut");
                //还原原来用户在线状态，并提示错误框
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_DELEGATE(
                                                                         UserProcessModel.ReSetUserOnlineStateToOrg));
            } 
        }

        /// <summary>
        ///     处理回馈更新用户分组请求包
        /// </summary>
        /// <param name="inpackage">回馈更新用户分组请求包</param>
        public static void Process_REPLY_UPDATE_ITEM(UpdateItemReplyPacket inpackage)
        {
            //卸载超时器
            Globle.timeoutTimerManager.RemoveTimer("UpdateItem" + inpackage.GetItemId());
            //修改成功
            if(inpackage.GetUpdateState())
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new UPDATE_ITEM(FriendTreeViewModel.SetItemName),
                                                                     inpackage);
            }
            //修改失败
            else
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_STRING_DELEGATE(ShowNotifyWindow),
                                                                     "更新分组名称失败 !");
            }
        }

        /// <summary>
        ///     处理回馈添加用户分组请求包
        /// </summary>
        /// <param name="inpackage"></param>
        public static void Process_REPLY_ADD_NEW_ITEM(AddItemReplyPacket inpackage)
        {
            //卸载添加分组超时器
            Globle.timeoutTimerManager.RemoveTimer(inpackage.GetItemKey());
            //添加成功
            if (inpackage.GetUpdateState())
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                 new SET_ADD_ITEM(FriendTreeViewModel.SetAddItem),
                                                                 inpackage);
            }
            //添加失败
            else
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                 new RETURN_ITEM(FriendTreeViewModel.ReturnItem),
                                                                 inpackage.GetItemKey());
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_STRING_DELEGATE(ShowNotifyWindow),
                                                                     "添加用户分组失败 !");
            }
        }

         /// <summary>
        ///     处理回馈删除用户分组请求包
        /// </summary>
        /// <param name="inpackage"></param>
        public static void Process_REPLY_DELETE_ITEM(DeleteItemReplyPacket inpackage)
        {
            //卸载超时器
             Globle.timeoutTimerManager.RemoveTimer("DeleteItem" + inpackage.GetItemId());
            //移除对应缓存
            DeclareCacheManager.itemControlCacheManager.RemoveItemCache("DeleteItem" + inpackage.GetItemId());
             //删除成功
             if (inpackage.GetUpdateState())
             {
                 System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                 new SET_DELETE_ITEM(FriendTreeViewModel.SetDeleteItem),
                                                                 inpackage);
             }
             //删除失败
             else
             {
                 //提示错误
                 System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_STRING_DELEGATE(ShowNotifyWindow),
                                                                     "删除用户分组失败 !");
             }
        }

        /// <summary>
        ///     处理获取要添加好友信息请求包 - 没有找到该好友
        /// </summary>
        /// <param name="inpackage">获取要添加好友信息请求包 - 没有找到该好友</param>
        public static void Process_REPLY_GET_POSTFRIENDINFO_NOT_FIND(GetPostFriendInfoNotFind_ReplyPacket inpackage)
        {
            //卸载超时器
            Globle.timeoutTimerManager.RemoveTimer("GetPostFriendInfo" + inpackage.GetFriendName());
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new GET_POSTFRIEND_INFO_NOTFIND(FriendViewModel.View_GetPostFriendInfo_NotFind), inpackage);
        }

        // <summary>
        ///     处理获取要添加好友信息请求包
        /// </summary>
        /// <param name="inpackage">获取要添加好友信息请求包</param>
        public static void Process_REPLY_GET_POSTFRIENDINFO(GetPostFriendInfo_ReplyPacket inpackage)
        {
            //卸载超时器
            Globle.timeoutTimerManager.RemoveTimer("GetPostFriendInfo" + inpackage.GetValueByKey("USERNAME"));
            System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new GET_POSTFRIEND_INFO(FriendViewModel.View_GetPostFriendInfo), inpackage);
        }

        /// <summary>
        ///     处理转发的好友申请
        /// </summary>
        public static void Process_REDIRECT_ADD_FRIEND(RedirectAddFriendPacket inpackage)
        {
            //////////////////////////////////////////////////////////////////////////////////////////////////
            ///                 开始触发任务栏图标
            ///////////////////////////////////////////////////////////////////////////////////////////////////
            Notify_AddFriend notify_AddFriend = new Notify_AddFriend("AddFriendPost: " + inpackage.GetUsername() + DateTime.Now.ToString(), inpackage, Hi_Baidu.Notify.NotifyType.NOTIFYTYPE.POST_ADD_FRIEND);
            Globle.systemNotifyIconManager.Add(notify_AddFriend);
            Globle.systemNotifyIconManager.Start();
            MusicPlayer.MusicPlayer.Play(Hi_Baidu.MusicPlayer.MusicPlayer.SongType.SYSTEM_MESSAGE);
        }

        /// <summary>
        ///     处理添加好友结果发送成功回馈包
        /// </summary>
        /// <param name="inpackage">添加好友结果发送成功回馈包</param>
        public static void Process_RETURN_POST_ADD_FRIEND_RESULT(PostAddFriendResultReplyPacket inpackage)
        {
            Globle.timeoutTimerManager.RemoveTimer("PostAddFriendResult:" + inpackage.GetFriendName());
        }

        /// <summary>
        ///     处理回馈添加好友最终状态的广播信息包
        /// </summary>
        /// <param name="inpackage">添加好友最终状态的广播信息包</param>
        public static void Process_BOARDCAST_ADD_FRIEND(BoardCast_AddFriendReplyPacket inpackage)
        {
            //添加成功
            if (inpackage.GetAddState())
            {
                //找到当前好友树中的对应分组
                Item item = Globle.itemCollection.GetMemberByKey(inpackage.GetItemId()) as Item;
                if(item != null)
                {
                    Friend friend = new Friend(inpackage.GetFriendName());
                    friend.OnlineState = true;
                    friend.Username = inpackage.GetFriendName();
                    friend.ForItem = item;
                    //加入该好友
                    Globle.friendCollection.Add(friend);
                    //向指定分组下加入好友
                    System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new FRIEND_DELEGATE(MainBoardViewModel.AddFriend), friend.ForItem.Key, new Object[] { friend });
                    //开始向服务器提交获取请求
                    GetInfomationModel.GetFriendInfo(Encoding.Default.GetBytes(inpackage.GetUsername()), BitConverter.GetBytes(int.Parse(inpackage.GetFriendId())));
                    ////////////////////////////////////////////////////////////////
                    ///    开始为每个好友获取详细信息的动作创建超时器
                    ////////////////////////////////////////////////////////////////
                    //创建超时器
                    GetFriendInfoTimeOutEvent GFIT = new GetFriendInfoTimeOutEvent(Encoding.Default.GetBytes(inpackage.GetUsername()), BitConverter.GetBytes(int.Parse(inpackage.GetFriendId())), inpackage.GetFriendId(), 5000, 2);
                    //安装超时器
                    Globle.timeoutTimerManager.InstallTimer(GFIT);
                    MusicPlayer.MusicPlayer.Play(Hi_Baidu.MusicPlayer.MusicPlayer.SongType.ONLINE);
                }
            }
            //添加失败
            else
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                     new PUBLIC_STRING_DELEGATE(ShowNotifyWindow),
                                                                     "用户: " + inpackage.GetFriendName() + " 拒绝了您的好友邀请!");
            }
        }

        /// <summary>
        ///     处理好友上线信息包
        /// </summary>
        /// <param name="inpackage">好友上线信息包</param>
        public static void Process_BOARDCAST_USERONLINE_MESSAGE(Boardcast_UserOnlinePacket inpackage)
        {
            Friend friend = Globle.friendCollection.GetMemberByKey(inpackage.GetFriendName()) as Friend;
            if (friend != null)
            {
                friend.TalkPort = inpackage.GetTalkPort();
                friend.VideoPort = inpackage.GetVideoPort();
                friend.VoicePort = inpackage.GetVoicePort();
                friend.FilePort = inpackage.GetFilePort();
                friend.IpAddress = inpackage.GetIpAddress();
                //调用中间层更新好友在线状态 
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new CHANAGE_FRIEND_STATE(MainBoardViewModel.ChanageFriendOnlineState), friend);
            }
        }

        public static void Process_DeleteFriend(DeleteFriendReplyPacket inpackage)
        {
            //删除成功
            if (inpackage.GetDeleteState())
            {
                //从界面中删除该好友
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new DELETE_FRIEND(MainBoardViewModel.DeleteFriend), inpackage);
            }
            //删除失败
            else
            {
                System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                                    new PUBLIC_STRING_DELEGATE(ShowNotifyWindow),
                                                                    "好友: " + inpackage.GetFriendName() + " 删除失败!");
            }
        }

        /// <summary>
        ///     处理回馈用户心跳请求包
        /// </summary>
        /// <param name="inpackage">回馈用户心跳请求包</param>
        public static void Process_REPLY_USER_HEART_BREATH(UserHeartBreathReplyPacket inpackage)
        {
            //心跳成功
            if(inpackage.GetHeartBreathState())
            {
                //卸载超时器
                Globle.timeoutTimerManager.RemoveTimer("Reply_UserHeartBreath");
            }
        }

        /// <summary>
        ///     从用户挂起的更新集合中找到更新后的新值
        /// </summary>
        /// <param name="DB_FILED" type="string">
        ///     <para>
        ///         数据库关联字段
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回 null, 则表示该记录不存在
        /// </returns>
        private static byte[] GetNewValueInUserInfo(String DB_FILED)
        {
            IMetaData type_UpdateUserInfo = Globle.updateUserInfoManager.GetMemberByKey(DB_FILED);
            if (type_UpdateUserInfo != null)
            {
                return Encoding.Default.GetBytes(((TYPE_UPDATE_USERINFO)type_UpdateUserInfo).New_Value);
            }
            return null;
        }
    }
}
